package com.hongxun.design.utils.dragutil;

import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by zerdoor_pc on 2016/5/12.
 */
public class DragUtil {
    Context context;
    View view;
    private int screenHeight;
    private int screenWidth;

    public DragUtil(Context context, View view) {
        this.context = context;
        this.view = view;

    }


    public void setDrag() {
        screenHeight = ((Activity) context).getWindowManager().getDefaultDisplay()
                .getHeight();
        screenWidth = ((Activity) context).getWindowManager().getDefaultDisplay()
                .getWidth();
//        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);

//        int lastx = this.sp.getInt("lastx", 0);
//        int lasty = this.sp.getInt("lasty", 0);
//
//        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
//        params.leftMargin = lastx;
//        params.topMargin = lasty;
//        view.setLayoutParams(params);

        view.setOnTouchListener(new View.OnTouchListener() {
            int startX;
            int startY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:// 手指第一次触摸到屏幕
                        this.startX = (int) event.getRawX();
                        this.startY = (int) event.getRawY();

                        break;
                    case MotionEvent.ACTION_MOVE:// 手指移动
                        int newX = (int) event.getRawX();
                        int newY = (int) event.getRawY();

                        int dx = newX - this.startX;
                        int dy = newY - this.startY;

                        // 计算出来控件原来的位置
                        int l = view.getLeft();
                        int r = view.getRight();
                        int t = view.getTop();
                        int b = view.getBottom();
                        int newt = t + dy;
                        int newb = b + dy;
                        int newl = l + dx;
                        int newr = r + dx;
                        if ((newl < 0) || (newt < 0)
                                || (newr > screenWidth)
                                || (newb > screenHeight)) {

                            break;
                        }  // 更新iv在屏幕的位置.
                        view.layout(newl, newt, newr, newb);
                        this.startX = (int) event.getRawX();
                        this.startY = (int) event.getRawY();

                        break;
                    case MotionEvent.ACTION_UP: // 手指离开屏幕的一瞬间
                        int lastx = view.getLeft();
                        int lasty = view.getTop();
//                        SharedPreferences.Editor editor = sp.edit();
//                        editor.putInt("lastx", lastx);
//                        editor.putInt("lasty", lasty);
//                        editor.commit();
                        break;
                }
                return true;
            }
        });
    }
}
